<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="1. 什么是箭头函数1.1. 箭头函数简介箭头函数的语法非常简单 (parameters) =&amp;gt; { statements }，箭头函数只能用赋值式写法，不能用声明式写法。看一下最简单的箭头函数表示法：12let sayHello = () =&amp;gt; &amp;#123; console.log(&apos;Hello World&apos;); &amp;#125;sayHello(); // Hello World 1.">
<meta property="og:type" content="article">
<meta property="og:title" content="ECMAScript6 之箭头函数™">
<meta property="og:url" content="http://xiezixi.oschina.io/2018/06/04/ES6-箭头函数/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="1. 什么是箭头函数1.1. 箭头函数简介箭头函数的语法非常简单 (parameters) =&amp;gt; { statements }，箭头函数只能用赋值式写法，不能用声明式写法。看一下最简单的箭头函数表示法：12let sayHello = () =&amp;gt; &amp;#123; console.log(&apos;Hello World&apos;); &amp;#125;sayHello(); // Hello World 1.">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-06-04T07:42:46.209Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ECMAScript6 之箭头函数™">
<meta name="twitter:description" content="1. 什么是箭头函数1.1. 箭头函数简介箭头函数的语法非常简单 (parameters) =&amp;gt; { statements }，箭头函数只能用赋值式写法，不能用声明式写法。看一下最简单的箭头函数表示法：12let sayHello = () =&amp;gt; &amp;#123; console.log(&apos;Hello World&apos;); &amp;#125;sayHello(); // Hello World 1.">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://xiezixi.oschina.io/2018/06/04/ES6-箭头函数/"/>





  <title>ECMAScript6 之箭头函数™ | 个人博客</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">曦瓜皮</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://xiezixi.oschina.io/blog/2018/06/04/ES6-箭头函数/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="曦瓜皮">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ECMAScript6 之箭头函数™</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-04T15:42:46+08:00">
                2018-06-04
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/ECMAScript6/" itemprop="url" rel="index">
                    <span itemprop="name">ECMAScript6</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-什么是箭头函数"><a href="#1-什么是箭头函数" class="headerlink" title="1. 什么是箭头函数"></a>1. 什么是箭头函数</h2><h3 id="1-1-箭头函数简介"><a href="#1-1-箭头函数简介" class="headerlink" title="1.1. 箭头函数简介"></a>1.1. 箭头函数简介</h3><p>箭头函数的语法非常简单 <code>(parameters) =&gt; { statements }</code>，箭头函数只能用赋值式写法，不能用声明式写法。看一下最简单的箭头函数表示法：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> sayHello = <span class="function"><span class="params">()</span> =&gt;</span> &#123; <span class="built_in">console</span>.log(<span class="string">'Hello World'</span>); &#125;</span><br><span class="line">sayHello(); <span class="comment">// Hello World</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-2-箭头函数的特点"><a href="#1-2-箭头函数的特点" class="headerlink" title="1.2. 箭头函数的特点"></a>1.2. 箭头函数的特点</h3><p>和普通函数相比，箭头函数主要就是以下三个方面的特点：</p>
<ul>
<li>更简化的代码语法</li>
<li>没有局部 this 的绑定</li>
<li>不绑定 arguments</li>
</ul>
<h2 id="2-更简化的代码语法"><a href="#2-更简化的代码语法" class="headerlink" title="2. 更简化的代码语法"></a>2. 更简化的代码语法</h2><h3 id="2-1-一般写法"><a href="#2-1-一般写法" class="headerlink" title="2.1. 一般写法"></a>2.1. 一般写法</h3><p><code>(parameters) =&gt; { statements }</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 求两个数的和并在控制台输出</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> sum = <span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a + b);</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> sum = <span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a + b);</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="2-2-省略-的情况"><a href="#2-2-省略-的情况" class="headerlink" title="2.2. 省略 () 的情况"></a>2.2. 省略 () 的情况</h3><p>如果只有一个参数，可以省略<code>()</code>括号。<br><code>parameters =&gt; { statements }</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 求一个数的两倍，并输出</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> double = <span class="function"><span class="params">num</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(num * <span class="number">2</span>);</span><br><span class="line">  <span class="keyword">return</span> num * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> double = <span class="function"><span class="keyword">function</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(num * <span class="number">2</span>);</span><br><span class="line">  <span class="keyword">return</span> num * <span class="number">2</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="2-3-省略-的情况"><a href="#2-3-省略-的情况" class="headerlink" title="2.3. 省略 {} 的情况"></a>2.3. 省略 {} 的情况</h3><p>如果返回值仅仅只有一个表达式（expression）, 还可以省略大括号<code>{}</code>。<br><code>parameters =&gt; statements</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 求一个数的两倍</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> double = <span class="function"><span class="params">num</span> =&gt;</span> num * <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> double = <span class="function"><span class="keyword">function</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> num * <span class="number">2</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="2-4-特殊情况"><a href="#2-4-特殊情况" class="headerlink" title="2.4. 特殊情况"></a>2.4. 特殊情况</h3><p>如果箭头函数直接返回一个对象，必须在对象外面加上括号。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> returnObj = <span class="function"><span class="params">()</span> =&gt;</span> (&#123;<span class="attr">name</span>: <span class="string">"xiguapi"</span>, <span class="attr">age</span>: <span class="number">23</span>&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> returnObj = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        name: <span class="string">"xiguapi"</span>,</span><br><span class="line">        age: <span class="number">23</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h2 id="3-没有局部-this-的绑定"><a href="#3-没有局部-this-的绑定" class="headerlink" title="3. 没有局部 this 的绑定"></a>3. 没有局部 this 的绑定</h2><p>和一般的函数不同，箭头函数不会绑定 <code>this</code>。 或则说箭头函数不会改变 <code>this</code> 本来的绑定。<br>我们用一个例子来说明：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Counter</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.num = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Counter(); <span class="comment">// a.num 0</span></span><br></pre></td></tr></table></figure></p>
<p>因为使用了关键字 <code>new</code> 构造，<code>Count()</code> 函数中的 <code>this</code>绑定到一个新的对象，并且赋值给 <code>a</code>。通过 <code>console.log</code> 打印 <code>a.num</code>，会输出 0。</p>
<p>如果我们想每过一秒将 <code>a.num</code> 的值加 1，该如何实现呢？可以使用 <code>setInterval()</code> 函数。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Counter</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.num = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">this</span>.timer = setInterval(<span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.num++;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.num);</span><br><span class="line">  &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> Counter();</span><br><span class="line"><span class="comment">// NaN</span></span><br><span class="line"><span class="comment">// NaN</span></span><br><span class="line"><span class="comment">// NaN</span></span><br><span class="line"><span class="comment">// ...</span></span><br></pre></td></tr></table></figure></p>
<p>首先函数 <code>setInterval</code> 没有被某个声明的对象调用，也没有使用 <code>new</code> 关键字，再之没有使用 <code>bind</code>, <code>call</code> 和 <code>apply</code>。<code>setInterval</code> 只是一个普通的函数。实际上 <code>setInterval</code> 里面的 <code>this</code> 绑定到全局对象 <code>window</code> 上了。</p>
<p>使用箭头函数！使用箭头函数就不会导致 <code>this</code> 被绑定到全局对象。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">function Counter() &#123;</span><br><span class="line">  this.num = 0;</span><br><span class="line">  this.timer = setInterval(() =&gt; &#123;</span><br><span class="line">    this.num++;</span><br><span class="line">    console.log(this.num);</span><br><span class="line">  &#125;, 1000);</span><br><span class="line">&#125;</span><br><span class="line">var b = new Counter();</span><br><span class="line">// 1</span><br><span class="line">// 2</span><br><span class="line">// 3</span><br><span class="line">// ...</span><br></pre></td></tr></table></figure></p>
<p>通过 <code>Counter</code> 构造函数绑定的 <code>this</code> 将会被保留。在 <code>setInterval</code> 函数中，<code>this</code> 依然指向我们新创建的 <code>b</code> 对象。</p>
<h2 id="3-不绑定-arguments"><a href="#3-不绑定-arguments" class="headerlink" title="3. 不绑定 arguments"></a>3. 不绑定 arguments</h2><p>箭头函数还有一个比较有特点的地方就是其不绑定 arguments,即如果你在箭头函数中使用 arguments 参数不能得到想要的内容。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrowfunc = <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="built_in">arguments</span>.length)</span><br><span class="line">arrowfunc()</span><br><span class="line"><span class="comment">// arguments is not defined</span></span><br></pre></td></tr></table></figure></p>
<h2 id="4-注意点"><a href="#4-注意点" class="headerlink" title="4. 注意点"></a>4. 注意点</h2><ul>
<li>箭头函数适合于无复杂逻辑或者无副作用的纯函数场景下，例如：用在 map、reduce、filter 的回调函数定义中</li>
<li>箭头函数的亮点是简洁，但在有多层函数嵌套的情况下，箭头函数反而影响了函数的作用范围的识别度，这种情况不建议使用箭头函数</li>
<li>箭头函数要实现类似纯函数的效果，必须剔除外部状态。所以箭头函数不具备普通函数里常见的 this、arguments 等，当然也就不能用 call()、apply()、bind() 去改变 this 的指向</li>
<li>箭头函数不适合定义对象的方法（对象字面量方法、对象原型方法、构造器方法），因为箭头函数没有自己的 this，其内部的 this 指向的是外层作用域的 this</li>
<li>箭头函数不适合定义结合动态上下文的回调函数（事件绑定函数），因为箭头函数在声明的时候会绑定静态上下文</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> json = &#123;<span class="attr">bar</span>: <span class="number">1</span>, <span class="attr">fn</span>: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="keyword">this</span>.bar)&#125;;</span><br><span class="line">json.fn(); <span class="comment">//-&gt; undefined</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.bar = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Foo.prototype.fn = <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="keyword">this</span>.foo);</span><br><span class="line"><span class="keyword">const</span> foo = <span class="keyword">new</span> Foo();</span><br><span class="line">foo.fn(); <span class="comment">//-&gt; undefined // this 并不是指向 Foo，根据变量查找规则，回溯到了全局作用域</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Message = <span class="function">(<span class="params">text</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">this</span>.text = text;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> helloMessage = <span class="keyword">new</span> Message(<span class="string">'Hello World!'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(helloMessage.text); <span class="comment">//-&gt; Message is not a constructor // 不可以当作构造函数，也就是说，不可以使用 new 命令</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> button = <span class="built_in">document</span>.querySelector(<span class="string">'button'</span>);</span><br><span class="line">button.addEventListener(<span class="string">'click'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">this</span>.textContent = <span class="string">'Loading...'</span>;</span><br><span class="line">&#125;); <span class="comment">// this 并不是指向预期的 button 元素，而是 window</span></span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/06/04/JS-ES6常用技巧/" rel="next" title="ECMAScript6 之常用技巧">
                <i class="fa fa-chevron-left"></i> ECMAScript6 之常用技巧
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/06/06/JS-原型链/" rel="prev" title="JavaScript 之原型到原型链™">
                JavaScript 之原型到原型链™ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.jpg"
               alt="曦瓜皮" />
          <p class="site-author-name" itemprop="name">曦瓜皮</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        

      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-什么是箭头函数"><span class="nav-number">1.</span> <span class="nav-text">1. 什么是箭头函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-箭头函数简介"><span class="nav-number">1.1.</span> <span class="nav-text">1.1. 箭头函数简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-箭头函数的特点"><span class="nav-number">1.2.</span> <span class="nav-text">1.2. 箭头函数的特点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-更简化的代码语法"><span class="nav-number">2.</span> <span class="nav-text">2. 更简化的代码语法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-一般写法"><span class="nav-number">2.1.</span> <span class="nav-text">2.1. 一般写法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-省略-的情况"><span class="nav-number">2.2.</span> <span class="nav-text">2.2. 省略 () 的情况</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-省略-的情况"><span class="nav-number">2.3.</span> <span class="nav-text">2.3. 省略 {} 的情况</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-特殊情况"><span class="nav-number">2.4.</span> <span class="nav-text">2.4. 特殊情况</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-没有局部-this-的绑定"><span class="nav-number">3.</span> <span class="nav-text">3. 没有局部 this 的绑定</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-不绑定-arguments"><span class="nav-number">4.</span> <span class="nav-text">3. 不绑定 arguments</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-注意点"><span class="nav-number">5.</span> <span class="nav-text">4. 注意点</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">曦瓜皮</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  

  

  

  

  

</body>
</html>
